package edu.gatech.cc.CFAWaffleFriesWheresMyStuff.gui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.R;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.Datastore.UserDoesNotExistException;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.DatastoreFactory;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.User;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.UserManager;

/**
 * Shows the homescreen layout with a menu for the app's abilities.
 * @author Matthew Barulic
 *
 */
public class HomeScreenActivity extends Activity {

	private User _user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_screen);
		Bundle extras = getIntent().getExtras();
		
		try {
			_user = (new UserManager(DatastoreFactory.getDatastore(getApplicationContext()))).getUser(extras.getString("user"));
		} catch (UserDoesNotExistException e) {
			e.printStackTrace();
		}
		
		final HomeScreenActivity me = this;
		
		((TextView)findViewById(R.id.homescreenWelcomeText)).setText("Welcome " + _user.getUsername() + "!" );
		
		((ImageButton)findViewById(R.id.homescreenLogoutButton)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				me.logout();
			}
		});
		
		((ImageButton)findViewById(R.id.homescreenAddNewItemButton)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				me.switchToAddItemScreen();
			}
		});
		
		((ImageButton)findViewById(R.id.homescreenMyItemsButton)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				me.switchToMyItems();
			}
		});
		
		((ImageButton)findViewById(R.id.homescreenSettingsButton)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				me.switchToSettings();
			}
		});
		
		((ImageButton)findViewById(R.id.homescreenSearchButton)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				me.switchToSearch();
			}
		});
		
		((ImageButton)findViewById(R.id.homescreenNotificationsButton)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				me.switchToMatch();
			}
		});
	}

	@Override
	public void onBackPressed()
	{
		logout();
	}
	
	/**
	 * Logs out the logged in user and transitions to the login activity
	 */
	private void logout()
	{
		final HomeScreenActivity me = this;
		AlertDialog dialog = new AlertDialog.Builder(this).create();
		dialog.setTitle("Logout?");
		dialog.setMessage("Are you sure you want to logout?");
		dialog.setCancelable(false);
		dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i = new Intent(getApplicationContext(), LoginActivity.class);
				me.startActivity(i);
				me.finish();
			}
		});
		dialog.setButton(DialogInterface.BUTTON_NEGATIVE,  "No", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// Do nothing
			}
		});
		dialog.setIcon(android.R.drawable.ic_dialog_alert);
		dialog.show();
	}
	
	/**
	 * Transitions to the AddItem activity
	 * Does NOT end the home activity
	 */
	private void switchToAddItemScreen()
	{
		Intent i = new Intent(getApplicationContext(), AddItemActivity.class);
		i.putExtra("user", _user.getUsername());
		startActivity(i);
	}
	
	/**
	 * Starts an ItemList activity setup to show this user's items.
	 * Does NOT end the home activity
	 */
	private void switchToMyItems()
	{
		Intent i = new Intent(getApplicationContext(), ItemListActivity.class);
		i.putExtra("loggedIn", _user.getUsername());
		i.putExtra("user", _user.getUsername());
		i.putExtra("title", "My Items");
		startActivity(i);
	}
	
	/**
	 * Starts the setting activity
	 * Does NOT end the home activity
	 */
	private void switchToSettings() {
		Intent i = new Intent(getApplicationContext(), SettingsActivity.class);
		i.putExtra("user", _user.getUsername());
		startActivity(i);
	}
	
	/**
	 * Starts the search activity
	 * Does NOT end the home activity
	 */
	private void switchToSearch() {
		Intent i = new Intent(getApplicationContext(), SearchActivity.class);
		i.putExtra("user", _user.getUsername());
		startActivity(i);
	}
	
	/**
	 * Starts the Find Matches activity
	 * Does NOT end the home activity
	 */
	private void switchToMatch() {
		Intent i = new Intent(getApplicationContext(), FindMatchesActivity.class);
		i.putExtra("user", _user.getUsername());
		startActivity(i);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_home_screen, menu);
		return true;
	}
	
	@Override
	protected void onResume() {
		try {
			_user = DatastoreFactory.getDatastore(getApplicationContext()).getUser(_user.getUsername());
		} catch (UserDoesNotExistException e) {
			// This should never actually happen.
			e.printStackTrace();
		}
		super.onResume();
	}

}
